package com.misday.eatup;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Utility {
	public static final String EVENT_QUERY = "QUERY";

	public static void setListViewHeightBasedOnChildren(ListView listView) {
		//get the Adapter of ListView
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		int len = listAdapter.getCount(); //listAdapter.getCount() get items count
		for (int i = 0; i < len; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0); //calc the width and height of item view.
			totalHeight += listItem.getMeasuredHeight(); //sum all item view height
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();

		//listView.getDividerHeight(), get the divider height between items
		//params.height, ListView total height
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

		listView.setLayoutParams(params);
	}

	public static String getVersionName(Context context, Class cls) {
		try {
			ComponentName comp = new ComponentName(context, cls);

			PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);

			return pinfo.versionName;

		} catch (android.content.pm.PackageManager.NameNotFoundException e) {
			return null;
		}
	}
}
